# Maintainer:  Alexey Pavlov <alexpux@gmail.com>
# Contributor: Alexey Borzenkov <snaury@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Kirill Müller <krlmlr@mailbox.org>

_enable_ada=yes
_enable_objc=yes
_enable_rust=no
_enable_jit=yes
_threads="posix"
_realname=gcc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libs"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-fortran"
         $([[ "$_enable_ada" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-ada")
         $([[ "$_enable_objc" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-objc")
         $([[ "$_enable_rust" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-rust")
         $([[ "$_enable_jit" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-libgccjit")
        )
#_snapshot=20181214
pkgver=13.2.0
#_majorver=${pkgver:0:1}
#_sourcedir=${_realname}-${_majorver}-${_snapshot}
_sourcedir=${_realname}-${pkgver}
pkgrel=2
pkgdesc="GCC for the MinGW-w64"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             $([[ "$_enable_ada" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-ada")
             "${MINGW_PACKAGE_PREFIX}-binutils"
             "${MINGW_PACKAGE_PREFIX}-crt"
             "${MINGW_PACKAGE_PREFIX}-headers"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-isl"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "${MINGW_PACKAGE_PREFIX}-mpc"
             "${MINGW_PACKAGE_PREFIX}-mpfr"
             "${MINGW_PACKAGE_PREFIX}-windows-default-manifest"
             "${MINGW_PACKAGE_PREFIX}-winpthreads"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-autotools")
options=('!emptydirs') # '!strip' 'debug')
source=("https://ftp.gnu.org/gnu/gcc/${_realname}-${pkgver%%+*}/${_realname}-${pkgver}.tar.xz"{,.sig}
        #https://www.mirrorservice.org/sites/sourceware.org/pub/gcc/snapshots/${_majorver}-${_snapshot}/gcc-${_majorver}-${_snapshot}.tar.xz
        "gdbinit"
        0002-Relocate-libintl.patch
        0003-Windows-Follow-Posix-dir-exists-semantics-more-close.patch
        0005-Windows-Don-t-ignore-native-system-header-dir.patch
        0006-Windows-New-feature-to-allow-overriding.patch
        0007-Build-EXTRA_GNATTOOLS-for-Ada.patch
        0008-Prettify-linking-no-undefined.patch
        0011-Enable-shared-gnat-implib.patch
        0012-Handle-spaces-in-path-for-default-manifest.patch
        0014-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch
        0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
        0021-PR14940-Allow-a-PCH-to-be-mapped-to-a-different-addr.patch
        0140-gcc-diagnostic-color.patch
        0200-add-m-no-align-vector-insn-option-for-i386.patch
        0300-override-builtin-printf-format.patch
        2000-enable-rust.patch
        2001-fix-building-rust-on-mingw-w64.patch
        "2f7e7bfa3c6327793cdcdcb5c770b93cecd49bd0.patch::https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=2f7e7bfa3c6327793cdcdcb5c770b93cecd49bd0"
        "3eeb4801d6f45f6250fc77a6d3ab4e0115f8cfdd.patch::https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=3eeb4801d6f45f6250fc77a6d3ab4e0115f8cfdd"
        777aa930b106fea2dd6ed9fe22b42a2717f1472d.patch)
sha256sums=('e275e76442a6067341a27f04c5c6b83d8613144004c0413528863dc6b5c743da'
            'SKIP'
            'bce81824fc89e5e62cca350de4c17a27e27a18a1a1ad5ca3492aec1fc5af3234'
            '3fab890be4b0d4e9e309749c4de5ce00f069eee4fe6080baa100f768264e6218'
            '3fc35067823f87e48dd3bfd265e7593000dc110fa9ad5f16c81b896765fc897a'
            '57f0081655ba9a94ed7cc3d8b67a79aee8442a56613bd17f9809a7b0e68e43cd'
            'a3637466910fae6145c947095051e3b91b16e4715d4abddc474eeb00539e9dc2'
            '90d5cb570083f9dea4ac0f0f87e3e8d2230f5052e6f9b946061a20a224a9d195'
            '8a40ea004a803b7e6aabd6c58d859502a57c87755dbcfedbdfed161f2fb7eb62'
            '5ef4148acc4a2b7ed648d4fe75fab5545e84b4b93a12d6ba4e4ea6061dd635fa'
            'e98805ead7d78ee2a92f237894c4b2b7ddc1688e1b517d8c04f28d440202e40f'
            'fd9bdecb2bbc4796bbc9f00b708dac42ef9e3464a06d6d27e5475cee117de5be'
            'ad1f7b5e7afaaec008b7cbd14feea13a10989fa91bda7003af72d457619bb199'
            '6c272078340a27b3f147e497115b0a6e9fc0da720a2602f12b086524522caa59'
            'e0a5b470f49a29f20215cc9f9d04c1cb9969dff6f0e546542799d3a693ef1c84'
            'c34f9e71b5a092be1987ad4c65891742c74c9eb8ef6560100e751cd31375f579'
            'f73c8d1701762fed7d8102d17d8e4416a4cc5e600e297a89c2e1fe09cd743a1c'
            '693a91a863a706b3526af01c9de5ce8295c59ef0f56f009bcfbf79a93cba2728'
            'e78d51c908cd4e5c905c62e8350db1c609479bb95333c67f706b56974226fd94'
            '74c4693850f6c96992d43414f88e9f16ab1a5d1458f5e4dc76ac3ce435a80eef'
            '8dc297c658911ead58cd06407b853bbe82c2c3da1ad24cbcc7b01f3c7b5f666b'
            'b0a98a41fea4a68f15d35b44495a487183a4d85ea9cb70cd4a645b4ed6583122')
validpgpkeys=(F3691687D867B81B51CE07D9BBE43771487328A9  # bpiotrowski@archlinux.org
              86CFFCA918CF3AF47147588051E8B148A9999C34  # evangelos@foutrelis.com
              13975A70E63C361C73AE69EF6EEB81F8981C74C7  # richard.guenther@gmail.com
              D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62) # Jakub Jelinek <jakub@redhat.com>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_sourcedir}
  del_file_exists intl/canonicalize.c intl/canonicalize.h \
    intl/relocatex.c intl/relocatex.h

  apply_patch_with_msg \
    0002-Relocate-libintl.patch \
    0003-Windows-Follow-Posix-dir-exists-semantics-more-close.patch \
    0005-Windows-Don-t-ignore-native-system-header-dir.patch \
    0006-Windows-New-feature-to-allow-overriding.patch \
    0007-Build-EXTRA_GNATTOOLS-for-Ada.patch \
    0008-Prettify-linking-no-undefined.patch \
    0011-Enable-shared-gnat-implib.patch \
    0012-Handle-spaces-in-path-for-default-manifest.patch \
    0014-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch \
    0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch \
    0021-PR14940-Allow-a-PCH-to-be-mapped-to-a-different-addr.patch

  # Enable diagnostic color under mintty
  # based on https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261761687
  apply_patch_with_msg \
    0140-gcc-diagnostic-color.patch

  # workaround for AVX misalignment issue for pass-by-value arguments
  #   cf. https://github.com/msys2/MSYS2-packages/issues/1209
  #   cf. https://sourceforge.net/p/mingw-w64/discussion/723797/thread/bc936130/ 
  #  Issue is longstanding upstream at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  #  Potential alternative: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=939559
  # https://github.com/msys2/MINGW-packages/pull/8317#issuecomment-824548411
  apply_patch_with_msg \
    0200-add-m-no-align-vector-insn-option-for-i386.patch

  # Patch from https://gcc.gnu.org/pipermail/gcc-patches/2022-January/588341.html
  # Related bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95130
  apply_patch_with_msg \
    0300-override-builtin-printf-format.patch

  # https://salsa.debian.org/toolchain-team/gcc/-/blob/b953a3f9/debian/patches/rust-enabled.diff
  apply_patch_with_msg \
    2000-enable-rust.patch

  apply_patch_with_msg \
    2001-fix-building-rust-on-mingw-w64.patch

  # backport: https://github.com/msys2/MINGW-packages/issues/17599
  # https://inbox.sourceware.org/gcc-patches/a22433f5-b4d2-19b7-86a2-31e2ee45fb61@martin.st/T/
  apply_patch_with_msg \
    2f7e7bfa3c6327793cdcdcb5c770b93cecd49bd0.patch \
    3eeb4801d6f45f6250fc77a6d3ab4e0115f8cfdd.patch

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110315#c7
  apply_patch_with_msg \
    777aa930b106fea2dd6ed9fe22b42a2717f1472d.patch

  # do not expect ${prefix}/mingw symlink - this should be superceded by
  # 0005-Windows-Don-t-ignore-native-system-header-dir.patch .. but isn't!
  sed -i 's/${prefix}\/mingw\//${prefix}\//g' configure

  # change hardcoded /mingw prefix to the real prefix .. isn't this rubbish?
  # it might work at build time and could be important there but beyond that?!
  local MINGW_NATIVE_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -i "s#\\/mingw\\/#${MINGW_NATIVE_PREFIX//\//\\/}\\/#g" gcc/config/i386/mingw32.h
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--disable-libstdcxx-debug")
  else
    extra_config+=("--enable-libstdcxx-debug")
  fi

  case "${CARCH}" in
    i686)
      extra_config+=(
        "--disable-sjlj-exceptions"
        "--with-dwarf2"
      )
      LDFLAGS+=" -Wl,--large-address-aware"
      local _arch=pentium4
    ;;

    x86_64)
      local _arch=nocona
    ;;
  esac

  local _languages="c,lto,c++,fortran"
  if [ "$_enable_ada" == "yes" ]; then
    _languages+=",ada"
  fi
  if [ "$_enable_objc" == "yes" ]; then
    _languages+=",objc,obj-c++"
  fi
  if [ "$_enable_rust" == "yes" ]; then
    _languages+=",rust"
  fi
  if [ "$_enable_jit" == "yes" ]; then
    _languages+=",jit"
  fi

  # so libgomp DLL gets built despide static libdl
  export lt_cv_deplibs_check_method='pass_all'

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105507#c3
  # At least with mingw32 + dwarf-2 exceptions there can only be one libgcc in
  # process, or exceptions will no longer work. Since some of the gcc deps are
  # linked dynamically like gmp/zlib/zstd and those pull in libgcc we can't
  # allow libgcc to be linked statically. The default is "-static-libstdc++
  # -static-libgcc" for both, so we drop "-static-libgcc" here:
  extra_config+=(
    '--with-boot-ldflags="-static-libstdc++"'
    '--with-stage1-ldflags="-static-libstdc++"'
  )

  # In addition adaint.c does `#include <accctrl.h>` which pulls in msxml.h, hacky hack:
  CPPFLAGS+=" -DCOM_NO_WINDOWS_H"

  ../${_sourcedir}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-local-prefix=${MINGW_PREFIX}/local \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-native-system-header-dir=${MINGW_PREFIX}/include \
    --libexecdir=${MINGW_PREFIX}/lib \
    --enable-bootstrap \
    --enable-checking=release \
    --with-arch=${_arch} \
    --with-tune=generic \
    --enable-languages=${_languages} \
    --enable-shared \
    --enable-static \
    --enable-libatomic \
    --enable-threads=${_threads} \
    --enable-graphite \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-filesystem-ts \
    --enable-libstdcxx-time \
    --disable-libstdcxx-pch \
    --enable-lto \
    --enable-libgomp \
    --disable-libssp \
    --disable-multilib \
    --disable-rpath \
    --disable-win32-registry \
    --disable-nls \
    --disable-werror \
    --disable-symvers \
    --with-libiconv \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl}=${MINGW_PREFIX} \
    --with-pkgversion="Rev${pkgrel}, Built by MSYS2 project" \
    --with-bugurl="https://github.com/msys2/MINGW-packages/issues" \
    --with-gnu-as \
    --with-gnu-ld \
    "${extra_config[@]}"

  # While we're debugging -fopenmp problems at least.
  # .. we may as well not strip anything.
  if check_option "strip" "n"; then
    sed -i 's,^STRIP = .*$,STRIP = true,g'                   Makefile
    sed -i 's,^STRIP_FOR_TARGET=.*$,STRIP_FOR_TARGET=true,g' Makefile
  fi

  # https://gcc.gnu.org/onlinedocs/gccint/Makefile.html
  make -O STAGE1_CFLAGS="-O2" \
          all

  rm -rf ${srcdir}${MINGW_PREFIX}

  make DESTDIR=${srcdir} install
  if [ "$_enable_ada" == "yes" ]; then
    mv ${srcdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/adalib/*.dll ${srcdir}${MINGW_PREFIX}/bin/
  fi
}

package_gcc-libs() {
  pkgdesc="GNU Compiler Collection (libraries) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread")
  provides=("${MINGW_PACKAGE_PREFIX}-omp")

  # Licensing information

  # Part of the package is GCCRLE, part is LGPL2+, see README generation below.
  # Since the packaged GCCRLE libraries are also GPL3+, and LGPL2+ is compatible
  # with GPL3+, the whole package can be redistributed under GPL3+.
  license=(GPL3+ partial:'GCCRLE' partial:'LGPL2+')

  # We explain the licensing in this generated README file
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-libs"
  cat << ENDFILE > "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-libs/README"
The libgcc, libstdc++, libgomp and libatomic libraries are covered by
GPL3+ with the GCC Runtime Library Exception. The libquadmath library is covered
by LGPL2+. The package as a whole can be redistributed under GPL3+.
ENDFILE

  # License files
  install -Dm644 "${srcdir}/${_sourcedir}/COPYING3"        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING3"
  install -Dm644 "${srcdir}/${_sourcedir}/COPYING.LIB"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING.LIB"
  install -Dm644 "${srcdir}/${_sourcedir}/COPYING.RUNTIME" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING.RUNTIME"

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin

  cd ${srcdir}${MINGW_PREFIX}
  # libitm* and libvtv* are disbled until fixed
  cp bin/{libatomic*,libgcc*,libgomp*,libquadmath*,libstdc*}.dll ${pkgdir}${MINGW_PREFIX}/bin/
  if [ "$_enable_jit" == "yes" ]; then
    rm ${pkgdir}${MINGW_PREFIX}/bin/libgccjit-0.dll
  fi
}

package_gcc() {
  pkgdesc="GNU Compiler Collection (C,C++,OpenMP) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-binutils"
           "${MINGW_PACKAGE_PREFIX}-crt"
           "${MINGW_PACKAGE_PREFIX}-headers"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libiconv"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-windows-default-manifest"
           "${MINGW_PACKAGE_PREFIX}-winpthreads"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-base"
            "${MINGW_PACKAGE_PREFIX}-cc")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-base")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib,share}

  cd ${srcdir}${MINGW_PREFIX}
  cp bin/cpp.exe                                        ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcc.exe                                        ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcc.exe                                        ${pkgdir}${MINGW_PREFIX}/bin/cc.exe
  cp bin/gcc-ar.exe                                     ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcc-nm.exe                                     ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcc-ranlib.exe                                 ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcov.exe                                       ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/gcov-tool.exe                                  ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/c++.exe                                        ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/g++.exe                                        ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-c++.exe                         ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-g++.exe                         ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gcc-${pkgver%%+*}.exe           ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gcc.exe                         ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gcc.exe                         ${pkgdir}${MINGW_PREFIX}/bin/${MINGW_CHOST}-cc.exe
  cp bin/${MINGW_CHOST}-gcc-ar.exe                      ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gcc-nm.exe                      ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gcc-ranlib.exe                  ${pkgdir}${MINGW_PREFIX}/bin/

  #cp bin/{libgcc*,libgomp*,libquadmath*,libstdc*}.dll ${pkgdir}${MINGW_PREFIX}/bin/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/*.h        ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include-fixed   ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/install-tools   ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  #cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/plugin          ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/cc1.exe            ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/collect2.exe       ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/crt*.o             ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/liblto*            ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/libgcov*           ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/lto*.exe           ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/cc1plus.exe        ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/g++-mapper-server.exe ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/

  cp lib/libatomic*                                      ${pkgdir}${MINGW_PREFIX}/lib/
  cp lib/libgcc_*                                         ${pkgdir}${MINGW_PREFIX}/lib/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/libgcc*            ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/

  cp lib/libgomp*                                        ${pkgdir}${MINGW_PREFIX}/lib/
  # cp lib/libitm*                                         ${pkgdir}${MINGW_PREFIX}/lib/
  cp lib/libquadmath*                                    ${pkgdir}${MINGW_PREFIX}/lib/
  # cp lib/libvtv*                                         ${pkgdir}${MINGW_PREFIX}/lib/
  cp lib/libstdc++*                                      ${pkgdir}${MINGW_PREFIX}/lib/
  cp lib/libsupc++*                                      ${pkgdir}${MINGW_PREFIX}/lib/

  #mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/lib
  #cp ${srcdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/lib/libgcc_s.a ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  #cp lib/gcc/${MINGW_CHOST}/lib/libgcc_s.a                ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/lib/

  #cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/c++      ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/
  cp -r include/c++                                       ${pkgdir}${MINGW_PREFIX}/include/
  #cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/libstdc++*         ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  #cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/libsupc++*         ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/{doc,info,locale,man}
  #cp -r share/doc/gcc-${pkgver%%+*} ${pkgdir}${MINGW_PREFIX}/share/doc/
  cp share/info/cpp.info*                                ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/cppinternals.info*                       ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/gcc.info*                                ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/gccinstall.info*                         ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/gccint.info*                             ${pkgdir}${MINGW_PREFIX}/share/info/
  # cp share/info/libitm.info*                             ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/libgomp.info*                            ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/libquadmath.info*                        ${pkgdir}${MINGW_PREFIX}/share/info/

  #cp share/locale/* ${pkgdir}${MINGW_PREFIX}/share/locale/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/gcc-${pkgver%%+*}/python
  cp -r share/gcc-${pkgver%%+*}/python/libstdcxx             ${pkgdir}${MINGW_PREFIX}/share/gcc-${pkgver%%+*}/python/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/man/man1
  cp share/man/man1/cpp.1*                               ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man1/gcc.1*                               ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man1/gcov.1*                              ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man7/fsf-funding.7*                       ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man7/gfdl.7*                              ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man7/gpl.7*                               ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  cp share/man/man1/g++.1*                               ${pkgdir}${MINGW_PREFIX}/share/man/man1/

  # install plugins for binutils
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/bfd-plugins
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/*plugin*.dll       ${pkgdir}${MINGW_PREFIX}/lib/bfd-plugins/

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${pkgver%%+*}'|g' ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
}

package_gcc-libgfortran() {
  pkgdesc="GNU Compiler Collection (libgfortran) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  cd ${srcdir}${MINGW_PREFIX}
  cp bin/libgfortran*.dll                                ${pkgdir}${MINGW_PREFIX}/bin/
}

package_gcc-fortran() {
  pkgdesc="GNU Compiler Collection (Fortran) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libgfortran=${pkgver}-${pkgrel}")
  provides=("${MINGW_PACKAGE_PREFIX}-fc")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,share}

  cd ${srcdir}${MINGW_PREFIX}
  cp bin/gfortran.exe ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gfortran.exe ${pkgdir}${MINGW_PREFIX}/bin/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/finclude       ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/f951.exe          ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/libcaf_single.a   ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/libgfortran*                                   ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/man/man1
  cp share/man/man1/gfortran.1*                         ${pkgdir}${MINGW_PREFIX}/share/man/man1/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/info
  cp share/info/gfortran.info*                          ${pkgdir}${MINGW_PREFIX}/share/info/
}

package_gcc-ada() {
  pkgdesc="GNU Compiler Collection (Ada) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,share}

  cd ${srcdir}${MINGW_PREFIX}
  cp bin/gnat*.exe ${pkgdir}${MINGW_PREFIX}/bin/

  cp bin/{libgnarl*,libgnat*}.dll                       ${pkgdir}${MINGW_PREFIX}/bin/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/adainclude     ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/adalib         ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/gnat1.exe         ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/info
  cp share/info/gnat-style.info*                        ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/gnat_rm.info*                           ${pkgdir}${MINGW_PREFIX}/share/info/
  cp share/info/gnat_ugn.info*                          ${pkgdir}${MINGW_PREFIX}/share/info/
}

package_gcc-objc() {
  pkgdesc="GNU Compiler Collection (ObjC,Obj-C++) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib}

  cd ${srcdir}${MINGW_PREFIX}
  cp bin/libobjc*.dll ${pkgdir}${MINGW_PREFIX}/bin/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include
  cp -r lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/objc    ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/include/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/cc1obj.exe         ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/cc1objplus.exe     ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
  cp lib/libobjc.*                                       ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
}

package_gcc-rust() {
  pkgdesc="GNU Compiler Collection (Rust) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib}

  cd ${srcdir}${MINGW_PREFIX}
  cp bin/gccrs.exe ${pkgdir}${MINGW_PREFIX}/bin/
  cp bin/${MINGW_CHOST}-gccrs.exe ${pkgdir}${MINGW_PREFIX}/bin/

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}
  cp lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/rust1.exe          ${pkgdir}${MINGW_PREFIX}/lib/gcc/${MINGW_CHOST}/${pkgver%%+*}/
}

package_libgccjit() {
  pkgdesc="GNU Compiler Collection (libgccjit) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include}

  cd ${srcdir}${MINGW_PREFIX}

  cp bin/libgccjit-0.dll                                      ${pkgdir}${MINGW_PREFIX}/bin/
  cp include/libgccjit.h                                      ${pkgdir}${MINGW_PREFIX}/include/
  cp include/libgccjit++.h                                    ${pkgdir}${MINGW_PREFIX}/include/
  cp lib/libgccjit.dll.a                                      ${pkgdir}${MINGW_PREFIX}/lib/
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/info
  cp share/info/libgccjit.info                                ${pkgdir}${MINGW_PREFIX}/share/info/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
